﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using System.Data;
using System.Text;
namespace EDMS.Front
{
    /// <summary>
    /// Community 的摘要说明
    /// </summary>
    public class Community : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string xjbh = context.Request["xjbho"].ToString();        //当前选举编号
            string strSQL = "select xjphone,nr from Xjdj as a,Xjgg as b where a.xjbh='" + xjbh + "' and b.xjbh='" + xjbh + "'";
            DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSQL);
            if (dt.Rows.Count > 0)
            {
                string data = ToJson(dt, "jsondata");
                context.Response.Write(data);
            }
        }
        #region Datatable转换为Json
        /// <summary>    
        /// Datatable转换为Json    
        /// </summary>    
        /// <param name="table">Datatable对象</param>    
        /// <returns>Json字符串</returns>    
        #region DataTable转成Json
        /// <summary>   
        /// DataTable转成Json    
        /// </summary>   
        /// <param name="jsonName"></param>   
        /// <param name="dt"></param>   
        /// <returns></returns>   
        public static string ToJson(DataTable dt, string jsonName)
        {
            StringBuilder Json = new StringBuilder();
            if (string.IsNullOrEmpty(jsonName))
                jsonName = dt.TableName;
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Type type = dt.Rows[i][j].GetType();
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type));
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }
        #endregion


        #endregion
        #region 格式化字符型、日期型、布尔型
        /// <summary>   
        /// 格式化字符型、日期型、布尔型   
        /// </summary>   
        /// <param name="str"></param>   
        /// <param name="type"></param>   
        /// <returns></returns>   
        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            return str;
        }
        #endregion
        #region 过滤特殊字符
        /// <summary>   
        /// 过滤特殊字符   
        /// </summary>   
        /// <param name="s"></param>   
        /// <returns></returns>   
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        // string xjbh = context.Request["xjbho"].ToString();        //当前选举编号
        // //查询指定社区的联系方式
        // string phone = "";
        // using (DataClassesDataContext lqdb = new DataClassesDataContext())
        // {
        //     var result = from r in lqdb.Xjdj
        //                  where r.xjbh == xjbh
        //                  select r;
        //     foreach (var item in result)
        //     {
        //         phone = item.xjphone;
        //     }
        // }
        ////选举公告
        // string xjgg = "";
        // using (DataClassesDataContext lqDb = new DAL.DataClassesDataContext())
        // {
        //     var result = from r in lqDb.Xjgg
        //                  where r.xjbh == xjbh
        //                  select r;
        //     foreach (var item in result)
        //     {
        //         xjgg = item.nr;
        //     }
        // }

        // string[] Array = new string[2];
        // Array[0] = phone;
        // Array[1] = xjgg;
        // context.Response.Write(Array);

        #region
        //string name = context.Request["name"].ToString();
        //if (name != "")
        //{
        //    DataTable dt = new DataTable();
        //    dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select xjmc from Xjdj where xjmc ='" + name + "'");
        //    if (dt.Rows.Count > 0)
        //    {
        //        string data = dt.Rows[0]["xjmc"].ToString();
        //        context.Response.Write(data);
        //    }
        //    else
        //    {
        //        context.Response.Write("");
        //    }
        //}
        //string xjmc = context.Request["com"].ToString();
        //if (xjmc != "")
        //{
        //    DataTable dt = new DataTable();
        //    dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from Xjdj where xjmc ='" + xjmc + "'");
        //    if (dt.Rows.Count > 0)
        //    {
        //        context.Response.Write("success");
        //    }
        //    else
        //    {
        //        context.Response.Write("sorry");
        //    }
        //}
        #endregion
    }
}